package com.quickcancellation.crm.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quickcancellation.crm.config.LogAspect;
import com.quickcancellation.crm.pojo.dto.PageParamsDto;
import com.quickcancellation.crm.pojo.dto.ResponseDto;
import com.quickcancellation.crm.pojo.entity.Menu;
import com.quickcancellation.crm.service.IMenuService;
import com.quickcancellation.crm.utils.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2023-02-27
 */
@RestController
@CrossOrigin
@RequestMapping("/menu")
@Api(tags = "菜单(权限)管理")
@SaCheckLogin
@SaCheckRole("menu")
public class MenuController {

    @Resource
    private IMenuService menuService;

    @PostMapping("list")
    @LogAspect()
    @ApiOperation("列表查询")
    public ResponseDto list(@RequestBody PageParamsDto<Menu> params) {

        int pageNo = params.getPage();
        int limit = params.getLimit();
        Menu menuParam = params.getParams();

        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();

        if (null != menuParam) {
            if (StringUtils.hasLength(menuParam.getMenuType())) {

                queryWrapper.like("menuType", menuParam.getMenuType());
            }

            if (StringUtils.hasLength(menuParam.getMenuname())) {
                queryWrapper.like("menuname", menuParam.getMenuname());
            }
        }


        Page<Menu> page = new Page<>(pageNo, limit);

        Page<Menu> resultPage = menuService.page(page, queryWrapper);

        return ResponseUtil.success(resultPage.getTotal(), resultPage.getRecords());

    }

    @PostMapping("add")
    @LogAspect()
    @ApiOperation("添加菜单")
    public ResponseDto saveMenu(@RequestBody Menu menu) {
        if (menu.getParentId() != 0) {
            menu.setHasThird("N");
            if (menuService.list(new QueryWrapper<Menu>().eq("url", menu.getMenuname())).size() > 0) {
                return ResponseUtil.fail("url地址已存在");
            }
        }

        if (menuService.list(new QueryWrapper<Menu>().eq("menuname", menu.getMenuname())).size() > 0) {
            return ResponseUtil.fail("菜单名称已存在");
        }

        if (menuService.list(new QueryWrapper<Menu>().eq("menuType", menu.getMenuname())).size() > 0) {
            return ResponseUtil.fail("菜单类型已存在");
        }

        boolean result = menuService.save(menu);

        return result ? ResponseUtil.success() : ResponseUtil.fail("-512", "添加失败");
    }

    @PostMapping("delete")
    @LogAspect()
    @ApiOperation("删除菜单")
    public ResponseDto deleteMenu(@RequestBody Menu menu) {

        boolean result = menuService.removeById(menu);

        return result ? ResponseUtil.success() : ResponseUtil.fail("-512", "删除失败");
    }

    @PostMapping("/getParentMenu")
    @LogAspect
    @ApiOperation("获取所有父级菜单")
    public ResponseDto getParentMenu() {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("parentId", 0);

        queryWrapper.select("id", "menuType");

        List<Menu> showMenuList = new ArrayList<>();
        showMenuList.add(new Menu(0L,"父级"));
        List<Menu> menusList = menuService.list(queryWrapper);

        showMenuList.addAll(menusList);
        return ResponseUtil.success(showMenuList);
    }


}
